package com.tbit.uqbike.client.remote.sms.apistore;

import com.tbit.uqbike.client.remote.nameauth.APIStore;
import com.tbit.uqbike.client.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;


/**
 * @author Kang
 * @date 2018年9月3日 下午1:40:25
 * @description 寻程数据发送短信
 */
@Slf4j
public class ApiStoreMsgUtils {
    /**
     * 迪迪出行的Appkey
     */
    private static final String DD_APP_KEY = "774ee6a1fa2caa7ba547d220b4a68bea";
    /**
     * 电马出行的Appkey
     */
    private static final String DM_APP_KEY = "3f79150c6c2f9b9931880753fe0d5b8e";
    /**
     * 接口地址
     */
    private static final String URL = "http://v.apistore.cn/api/v14/xsend";//模板短信

    /**
     * 短信模板发送
     *
     * @param phone
     * @param code
     * @param
     */
    public static void registerMOdelSend(String phone, String code, Integer accountId) {
        //请求参数
        Map<String, Object> params = new HashMap<>();
        /**电马出行还是迪迪出行*/
        if (accountId.equals(202)) {
            params.put("key", DM_APP_KEY);
            params.put("tpl_id", 91933);
        } else if (accountId.equals(204)) {
            params.put("key", DD_APP_KEY);
            params.put("tpl_id", 91934);
        }
        params.put("mobile", phone);
        params.put("tpl_val", "{\"code\":\"" + code + "\"}");
        String json = APIStore.doPost(URL, APIStore.urlencode(params));
        System.out.println(json);
        if (json != null) {
            ApiStoreMsgResult result = JsonUtils.json2Obj(json, ApiStoreMsgResult.class);
            if (result.getError_code() != 0) {
                System.out.println(result.getError_code());
                log.info("[用户验证码]寻程短信发送失败,原因：" + result.getReason() + "，错误码：" + result.getError_code());
            }
        }
    }

    /**
     * 短信模板发送
     *
     * @param phone
     * @param code
     * @param
     */
    public static void send(String phone, String code, String appKey, Integer modelId) {
        //请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("key", appKey);
        params.put("tpl_id", modelId);
        params.put("mobile", phone);
        params.put("tpl_val", "{\"code\":\"" + code + "\"}");
        String json = APIStore.doPost(URL, APIStore.urlencode(params));
        System.out.println(json);
        if (json != null) {
            ApiStoreMsgResult result = JsonUtils.json2Obj(json, ApiStoreMsgResult.class);
            if (result.getError_code() != 0) {
                System.out.println(result.getError_code());
                log.info("[用户验证码]寻程短信发送失败,原因：" + result.getReason() + "，错误码：" + result.getError_code());
            }
        }

    }

    /**
     * 发送发现丢失车辆位置上报短信
     *
     * @param phone
     * @param userCode
     * @param appKey
     * @param modelId
     */
    public static void sendLostMachineAlarm(String phone, String userCode, String appKey, String modelId, String address) {

        // 请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("key", appKey);
        params.put("tpl_id", modelId);
        params.put("mobile", phone);
        params.put("tpl_val", "{\"userCode\":\"" + userCode + "\",\"address\":\"" + address + "\"}");
        String json = APIStore.doPost(URL, APIStore.urlencode(params));
    }

    /**
     * 骑行订单退款短信模板发送
     *
     * @param
     */
    public static void refundSend(String phone, String isMessage, String appKey, Integer modelId) {
        //请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("key", appKey);
        params.put("tpl_id", modelId);
        params.put("mobile", phone);
        params.put("tpl_val", "{\"isMessage\":\"" + isMessage + "\"}");
        String json = APIStore.doPost(URL, APIStore.urlencode(params));
        log.info(json);
        if (json != null) {
            ApiStoreMsgResult result = JsonUtils.json2Obj(json, ApiStoreMsgResult.class);
            if (result.getError_code() != 0) {
                log.info("[用户验证码]寻程短信发送失败,原因：" + result.getReason() + "，错误码：" + result.getError_code());
            }
        }

    }

    public static void main(String[] args) {
        registerMOdelSend("15079017029", "1023", 204);
    }
}
